package com.yaozheng.vocationaltraining.fragment.exam;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.adapter.PKMorePersonListAdapter;
import com.yaozheng.vocationaltraining.adapter.PKPlayerStatusAdapter;
import com.yaozheng.vocationaltraining.bean.PKMorePersonBean;
import com.yaozheng.vocationaltraining.bean.PKPlayerStatusBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.exam.IPKMorePersonHallView;
import com.yaozheng.vocationaltraining.service.exam.PKConfirmService;
import com.yaozheng.vocationaltraining.service.exam.PKMorePersonHallService;
import com.yaozheng.vocationaltraining.service.impl.exam.PKConfirmServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.exam.PKMorePersonHallServiceImpl;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by moon on 2017/11/16.
 */

@EFragment(R.layout.activity_pk_more_person_hall)
public class PKMorePersonHallFragment extends BaseFragment
        implements IPKMorePersonHallView {

    @ViewById
    ListView listView;
    PKMorePersonListAdapter pkMorePersonListAdapter;
    ArrayList<PKMorePersonBean> pkMorePersonListData = new ArrayList<>();

    @ViewById
    TextView headTitleText;

    @ViewById
    Button start_button;
    @ViewById
    Button leave_button;

    private BitmapLoader userBitmapLoader;
    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    @Bean(PKMorePersonHallServiceImpl.class)
    PKMorePersonHallService pkMorePersonHallService;

    CountDownTimer cdt;

    @AfterViews
    public void initView() {
        headTitleText.setText("多人PK");
        pkMorePersonHallService.init(this);
        userBitmapLoader = new BitmapLoader(Glide.with(this), R.drawable.user_img);



        pkMorePersonListAdapter = new PKMorePersonListAdapter(this
                ,pkMorePersonListData);
        listView.setAdapter(pkMorePersonListAdapter);

        cdt = new CountDownTimer(3600 * 1000, 500 ) {
            @Override
            public void onTick(long millisUntilFinished) {
                onRefresh();
            }
            @Override
            public void onFinish() {

            }
        };
        cdt.start();

        onRefresh();
    }

    private void onRefresh() {
        pkMorePersonHallService.list(gameCategoryId);
    }


    public int getGameCategoryId() {
        return gameCategoryId;
    }

    public void setGameCategoryId(int gameCategoryId) {
        this.gameCategoryId = gameCategoryId;
    }

    int gameCategoryId;

    @Override
    public void onStop() {
        super.onStop();
        if (cdt != null){
            cdt.cancel();
        }
    }

    @Click(R.id.start_button)
    public void startButtonClick() {//
        pkMorePersonHallService.launcherPKMorePerson(gameCategoryId);
    }


    @Click(R.id.leave_button)
    public void leaveButtonClick() {//
        //这里的离开，只是界面的跳转，返回到上一个界面

        getHomeActivity().backClick();
        //pkMorePersonHallService.leave(seatId);
    }





    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","rooms");
            if (dataList != null) {
                pkMorePersonListData.clear();

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject playerJsonObject = dataList.getJSONObject(i);
                    PKMorePersonBean bean = new PKMorePersonBean();
                    bean.setId(playerJsonObject.getInt("id"));
                    bean.setGameCateId(playerJsonObject.getInt("gameCateId"));
                    bean.setGameId(playerJsonObject.getInt("gameId"));
                    bean.setGameInitiatorId(playerJsonObject.getInt("gameInitiatorId"));
                    bean.setGameInitTime(playerJsonObject.getString("gameInitTime"));
                    bean.setGameStatus(playerJsonObject.getInt("gameStatus"));
                    bean.setRemaining(playerJsonObject.getInt("remaining"));
                    bean.setGameInitiatorName(playerJsonObject.getString("gameInitiatorName"));
                    bean.setGameInitiatorAvatar(playerJsonObject.getString("gameInitiatorAvatar"));

                    pkMorePersonListData.add(bean);
                }

                pkMorePersonListAdapter.setData(pkMorePersonListData);
                pkMorePersonListAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {

    }

    public void partInPK(int gameId,int roomId,int remaining_time) {
//        getHomeActivity().openPKMorePersonConfirmFragment(gameId,roomId, remaining_time);
        pkMorePersonHallService.partInPK(gameId,roomId,remaining_time);
    }

    @UiThread
    @Override
    public void parkInPKSuccess(JSONObject jsonObject,int gameId,int roomId,int remaining_time) {
        getHomeActivity().openPKMorePersonConfirmFragment(gameId,roomId, remaining_time);
    }

    @UiThread
    @Override
    public void parkInPKError(String error) {
        Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
    }

    @UiThread
    @Override
    public void launcherPKMorePersonSuccess(JSONObject jsonObject) {
        JSONObject dataObject2 = TypeUtils.getJsonObject(jsonObject,"data", "game");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//2017-11-08 22:54:48
        try {
            Date startTime = df.parse(dataObject2.getString("startTime"));
            int remainingTime = (int) ((startTime.getTime() -  System.currentTimeMillis()) / 1000);
            getHomeActivity().openPKMorePersonConfirmFragment(dataObject2.getInt("id"),
                    dataObject2.getInt("gameRoomId"),0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @UiThread
    @Override
    public void launcherPKMorePersonPKError(String error) {
        Toast.makeText(this.getActivity(),error,Toast.LENGTH_SHORT).show();
    }
}
